<%@page import="java.util.Random"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="database.DBConnector"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
            DBConnector dbc = DBConnector.getDBC();
            Random ran = new Random();
            String tlf = request.getParameter("tlf");
            String pw = request.getParameter("pw2");
            String fname = request.getParameter("fname");
            String lname = request.getParameter("lname");
            String email = request.getParameter("email");
            String streetname = request.getParameter("streetname");
            String streetnumber = request.getParameter("streetnumber");
            String zipcode = request.getParameter("zipcode");
            String city = request.getParameter("city");
            String country = request.getParameter("country");
            String captcha = request.getParameter("captchaInput");
            //String cvr = request.getParameter("lname");
            //String company = request.getParameter("company");
            boolean zipcodeExists = false;
            boolean cvrExists = false;
            if (session.getAttribute("VALID_TLF").equals("TRUE")
                    && session.getAttribute("VALID_PW").equals("TRUE")
                    && session.getAttribute("VALID_FNAME").equals("TRUE")
                    && session.getAttribute("VALID_LNAME").equals("TRUE")
                    && session.getAttribute("VALID_STREETNAME").equals("TRUE")
                    && session.getAttribute("VALID_STREETNUMBER").equals("TRUE")
                    && session.getAttribute("VALID_ZIPCODE").equals("TRUE")
                    && session.getAttribute("VALID_CITY").equals("TRUE")
                    && session.getAttribute("VALID_COUNTRY").equals("TRUE")
                    && session.getAttribute("CAPTCHA_CODE").equals(captcha)) {
                ResultSet rs = dbc.doQuery("SELECT zipcode FROM cities WHERE zipcode='" + zipcode + "';");
                while (rs.next()) {
                    zipcodeExists = true;
                }
                if (!zipcodeExists) {
                    dbc.doUpdate("INSERT INTO Cities VALUES('" + zipcode + "','" + city + "');");
                }
                String cvr = "" + ran.nextInt(1000000);
                ResultSet rs1 = dbc.doQuery("SELECT cvr FROM companies WHERE cvr='" + cvr + "';");
                while (rs1.next()) {
                    cvrExists = true;
                }
                if (!cvrExists) {
                    dbc.doUpdate("INSERT INTO Companies VALUES('" + cvr + "','Random Firmanavn');");
                }
                dbc.doUpdate("INSERT INTO customers VALUES('" + tlf + "','" + pw + "','" + fname + "','" + lname + "','" + email + "','" + streetname + "','" + streetnumber + "','" + zipcode + "','" + country + "','" + cvr + "');");
                response.sendRedirect("../Frontend/index.jsp");
            } else {
%> <br><br>Udfyld venligst formularen korrekt...<br><br>
<jsp:include page="../Frontend/newCustomer.jsp"></jsp:include>
<%}
%>
